package com.itheima.common;

import com.itheima.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @email 1572178030@qq.com
 * @author:黄海宾
 * @date: 2023/2/6  11:20
 *
 */

@RestControllerAdvice(annotations = {RestController.class, Controller.class})
/*@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody*/
@Slf4j
public class GlobalExceptionHandle {

    @ExceptionHandler(Exception.class)//SQLIntegrityConstraintViolationException
    public R<String> doHandle(Exception sc) {
        if (sc.getMessage().contains("Duplicate entry")) {
            String[] split = sc.getMessage().split(" ");
            String execMessage = split[2] + sc;
            return R.success(execMessage);
        }
        return R.error("错误信息");
    }


    @ExceptionHandler(CustomException.class)//SQLIntegrityConstraintViolationException
    public R<String> CustomExceptionHandle(CustomException sc) {
        log.error(sc.getMessage());
        return R.error(sc.getMessage());
    }
}
